--[[

   Copyright (C) 2007 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   Written by Charles Cahoon during Summer of Code 2007
--]]


require("lfs")

local DEBUG         = os.getenv('DEBUG')
local source        = arg[1]
local destination   = arg[2]

function print_debug (msg)
   if DEBUG then
       print(msg)
   end
end

function file_copy (src, dest, max_b )
   local max_bytes     = max_b or 65000
   if src == nil or dest == nil then
       print('source and destination file must be given')
       return nil
   end
   local src_handle, err = io.open(src, 'r')
   print_debug('opening '.. src)

   if err then
       print( 'error opening ' .. src .. ' - ' .. err )
       return nil
   end
   local dest_handle, err = io.open(dest, 'w')
   print_debug('opening '.. dest)
   if err then
       print( 'error opening ' .. dest .. ' - ' .. err )
       src_handle:close()
       return nil
   end
   local result
   local total_size =0
   local buffer, err = src_handle:read(max_bytes)
   while ( not err ) and buffer and (#buffer > 0) do
       print_debug('reading '.. #buffer )
       total_size = total_size + #buffer
       result, err = dest_handle:write(buffer)
       print_debug('writing '.. #buffer )
       if not err then
           buffer, err = src_handle:read(max_bytes)
       end
   end
   print_debug('closing files ' )
   src_handle:close()
   dest_handle:close()
   if err then
       print( 'error during the copy  - ' .. err )
       return nil
   end
   return total_size
end

function move_files()
	lfs.mkdir(destination .. "/tests/suite/mtc")
	lfs.mkdir(destination .. "/tests/suite/mtc/t")
	lfs.mkdir(destination .. "/tests/suite/mtc/r")
	local x, y, i, j, m, n, test_name, result_name, dest_path, mtc_name
	for file in lfs.dir(source) do
		i, j, result_name = string.find(file, "^s*(.+)\.lua$")
		if result_name then
			print(file)			
			dest_path = destination .. "/tests/suite/mtc/t/" .. file
			file = source .. "/" .. file
			local copied = file_copy(file,dest_path)			
			if copied then
   				print ('copied ' .. copied .. ' bytes' )
			end
		end
	end
	for file in lfs.dir(source .. "/test/t") do
		--COPIES TEST FILES
		x, y, test_name = string.find(file, "^%s*(.+)\.test$")		
		if test_name then
			print(file)
			dest_path = destination .. "/tests/suite/mtc/t/" .. file
			file = source .. "/test/t/" .. file
			local copied = file_copy(file,dest_path)
			if copied then
   				print ('copied ' .. copied .. ' bytes' )
			end
			print(test_name .. ".lua")		
			local copied2 = file_copy(destination .. "/tests/suite/mtc/t/mysql-test-creator.lua", destination .. "/tests/suite/mtc/t/" .. test_name .. ".lua")
			if copied2 then
				print ('copied ' .. copied .. ' bytes' )
			end			
 	    end
 	    --COPIES MTCT FILES
 	    x, y, test_name = string.find(file, "^%s*(.+)\.mtct$")		
		if test_name then
			print(file)
			dest_path = destination .. "/tests/suite/mtc/t/" .. file
			file = source .. "/test/t/" .. file
			local copied = file_copy(file,dest_path)
			if copied then
   				print ('copied ' .. copied .. ' bytes' )
			end
		end
	end
	for file in lfs.dir(source .. "/test/r") do
		--COPIES RESULT FILES
		i, j, result_name = string.find(file, "^s*(.+)\.result$")		
		if result_name then
			print(file)			
			dest_path = destination .. "/tests/suite/mtc/r/" .. file
			file = source .. "/test/r/" .. file
			local copied = file_copy(file,dest_path)			
			if copied then
   				print ('copied ' .. copied .. ' bytes' )
			end
		end
		--COPIES MCTR FILES
		i, j, result_name = string.find(file, "^s*(.+)\.mtcr$")
		if result_name then
			print(file)			
			dest_path = destination .. "/tests/suite/mtc/r/" .. file
			file = source .. "/test/r/" .. file
			local copied = file_copy(file,dest_path)			
			if copied then
   				print ('copied ' .. copied .. ' bytes' )
			end
		end
	end
end

function run_tests()
	local command = "LUA_CPATH='".. destination .. "/tests/.libs/?.so' lua tests/run-tests.lua tests/suite/mtc"
	local chdir = lfs.chdir(destination)
	if not chdir then
		print(chdir)
		os.exit()
	end
	print(command)
	os.execute(command)
end

--------------
--START HERE--
--------------
move_files()
--run_tests()

